package com.heima.page.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.message.NewsAutoScanConstants;
import com.heima.page.service.GeneratePageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class GeneratePageAutoListener {
    @Autowired
    GeneratePageService generatePageService;

    @KafkaListener(topics = NewsAutoScanConstants.GENERATE_PAGE_TOPIC)//监听对应主题
    public void receiveMessage(String message) {
        if (StringUtils.isNotBlank(message)) {
            log.info("====GeneratePageAutoListener recevieMessage :{}", message);
            //解析message
            Map map = JSON.parseObject(message, Map.class);
            Long articleId = (Long) map.get(NewsAutoScanConstants.ARTICLE_ID);
            if (articleId != null) {
                try {
                    //调用业务方法
                    generatePageService.generateArticlePage(articleId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            log.info("====generatePageService success====");
        }
    }
}
